package drawingBoard.action;

import drawingBoard.ImageFrame;
import drawingBoard.tool.AbstractTool;
import drawingBoard.tool.ToolFactory;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.event.ActionEvent;

/**
 * AbstractAction：Swing提供的抽象动作类，是Action接口的抽象实现
 * 类ImageAction作为AbstractAction的子类，用于选取具体工具或色块
 * 本类供
 */
public class ImageAction extends AbstractAction {  //按键处理类

	private ImageFrame frame = null;  //窗体
	private String name = "";  //绘图工具名

	private JPanel colorPanel = null;  //颜色面板
	private Color color = null;  //选中的颜色

	public ImageAction(ImageIcon icon, String name, ImageFrame frame) {  //用于绘图工具的构造器
		super("", icon);  //构造带图标的 Action对象
		this.name = name;
		this.frame = frame;
	}

	public ImageAction(Color color, JPanel colorPanel) {  //用于颜色的构造器
		super();
		this.color = color;
		this.colorPanel = colorPanel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {  //重写基类的抽象方法
		if (name != "") {  //选择绘图工具时
			frame.setTool(ToolFactory.getToolInstance(frame, name));
		}
		if (color != null) {  //选择颜色块时
			AbstractTool.color = color;
			colorPanel.setBackground(color);
		}
	}
}
